/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.store.dto;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.javaeye.common.dto.Exportable;

public class MaterialsSKUDetail implements Exportable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2844773041688424970L;

	private int id;
	
	private Materials materials;
	
	/**
	 * 正常库存
	 */
	private float number;
	
	/**
	 * 待检库存
	 */
	private float waitNumber;
	
	/**
	 * 不合格库存
	 */
	private float defectiveNumber;
	
	private int inuse;
	
	private String customFieldCombox;
	
	private List<MaterialsField> fields;
	
	public MaterialsSKUDetail() {
		materials = new Materials();
		fields = new ArrayList<MaterialsField>();
	}
	
	public void addField(MaterialsField field) {
		fields.add(field);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setMaterials(Materials materials) {
		this.materials = materials;
	}

	public Materials getMaterials() {
		return materials;
	}

	public void setFieldCombox(String fieldCombox) {
		String[] fields = fieldCombox.split(",");
		for (String s : fields) {
			String[] values = s.split(":");
			MaterialsField f = new MaterialsField();
			f.setFieldGroupId(values[0]);
			f.setId(values[1]);
			addField(f);
		}
	}

	public String getFieldCombox() {
		StringBuffer sb = new StringBuffer();
		MaterialsField f;
		int count = fields.size();
		for (int i = 0; i < count; i++) {
			f = fields.get(i);
			sb.append(f.getFieldGroupId());
			sb.append(":");
			sb.append(f.getId());
			if (i < count - 1) {
				sb.append(",");
			}
		}
		return sb.toString();
	}

	public void setFields(List<MaterialsField> fields) {
		this.fields = fields;
	}

	public List<MaterialsField> getFields() {
		return fields;
	}

	public void setInuse(int inuse) {
		this.inuse = inuse;
	}

	public int getInuse() {
		return inuse;
	}
	
	public boolean isInUse() {
		return inuse == 1;
	}

	public float getNumber() {
		return number;
	}

	public void setNumber(float number) {
		this.number = number;
	}

	public float getWaitNumber() {
		return waitNumber;
	}

	public void setWaitNumber(float waitNumber) {
		this.waitNumber = waitNumber;
	}

	public float getDefectiveNumber() {
		return defectiveNumber;
	}

	public void setDefectiveNumber(float defectiveNumber) {
		this.defectiveNumber = defectiveNumber;
	}
	
	@Override
	public Object getValue(int colIndex) {
		switch(colIndex) {
		case 0 :
			return getMaterials().getRootCategory().getName();
		case 1 :
			return getMaterials().getCategory().getName();
		case 2 :
			return getMaterials().getName();
		case 3 :
			return getMaterials().getType();
		case 4 :
			return getCustomFieldCombox();
		case 5 :
			return getMaterials().getUnits();
		case 6 :
			return getMaterials().getRootCategory().getId();
		case 7 :
			return getId();
		case 8 :
			return "A";
		case 9 :
			return "0";
		case 10 :
			return "0";
		case 11 :
			return "0";
		default:
			return null;
		}
	}
	
	@Override
	public String getColumnName(int colIndex) {
		return columnNames[colIndex];
	}
	
	@Override
	public int getColumnCount() {
		return columnNames.length;
	}
	
	public void setCustomFieldCombox(String customFieldCombox) {
		this.customFieldCombox = customFieldCombox;
	}

	/**
	 * 取得自定义组合的名称(显示用)
	 * @return
	 */
	public String getCustomFieldCombox() {
		if (customFieldCombox == null) {
			Set<MaterialsFieldCustom> customfields = getMaterials().getCustomFields();
			StringBuffer sb = new StringBuffer();
			for (MaterialsField f : fields) {
				String fileId = f.getId();
				f.setCustomName(getCustomName(fileId, customfields));
				sb.append(f.getCustomName());
				sb.append(" ");
			}
			customFieldCombox = sb.toString();
		}
		return customFieldCombox;
	}
	
	private String getCustomName(String fileId, Set<MaterialsFieldCustom> fields) {
		for (MaterialsFieldCustom cf : fields) {
			if (cf.getFieldId().equals(fileId)) {
				return cf.getValue();
			}
		}
		return "";
	}
	
	private String[] columnNames = {
			"大分类",
			"中分类",
			"名称",
			"规格",
			"其他",
			"单位",
			"category",
			"ID",
			"批次",
			"价格",
			"合格数",
			"不合格数"};
}
